package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.RegularAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.HomeTender;
import com.kkd.kkdapp.http.entity.selectedGroup;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class WelfareActivity extends BaseActivity implements OnClickListener,
		KKDScrollListener {

	private final int REFRESH = 2; // 当前页数小于总页数
	private final int REQUEST_WAS_ABORTED = 3; // 请求失败
	private RelativeLayout title_img; // 返回
	private ImageView img_null; // 空值或网络异常图片
	private TextView tv_null; // 空值或网络异常提示语
	private TextView title_tv;
	private RegularAdapter adapter;
	private KkdXlistView xlistview;
	private ListView mListView;
	private ArrayList<HomeTender> datalist = new ArrayList<HomeTender>();
	private boolean REFRESHFLAG = true;
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			String time = "";
			switch (msg.what) {
			case REFRESH:
				if (datalist.size() > 0) {
					adapter.notifyKkdDataSetChanged();
					xlistview.stopLoadMore(1);
					xlistview.stopRefresh();
					xlistview.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.GONE);
					img_null.setVisibility(View.GONE);
				} else {
					img_null.setImageResource(R.drawable.img_null);
					img_null.setVisibility(View.VISIBLE);
					tv_null.setText("暂无投资福利");
					tv_null.setVisibility(View.VISIBLE);
					xlistview.stopLoadMore(1);
					xlistview.stopRefresh();
					xlistview.setVisibility(View.GONE);
				}

				break;
			case REQUEST_WAS_ABORTED:
				xlistview.stopLoadMore(1);
				xlistview.stopRefresh();
				if (datalist == null || datalist.size() == 0) {
					img_null.setImageResource(R.drawable.img_null_net);
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						tv_null.setText("网络错误，请稍候下拉刷新重试");
					} else {
						tv_null.setText("服务异常，请稍候下拉刷新重试");
					}
					img_null.setVisibility(View.VISIBLE);
					tv_null.setVisibility(View.VISIBLE);
				}
				break;
			}

		}
	};
	private View tip_top;
	private TextView group_name;
	private TextView discrip;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welfare);
		initView(); // 初始化控件
		setListener(); // 注册监听时间
		whetherNetWork(); // 是否有网络
	}

	/****************
	 * 初始化控件
	 ****************/
	public void initView() {
		title_tv = (TextView) this.findViewById(R.id.title_tv);
		title_img = (RelativeLayout) this.findViewById(R.id.title_img);
		img_null = (ImageView) this.findViewById(R.id.img_null);
		tv_null = (TextView) this.findViewById(R.id.tv_null);
		xlistview = (KkdXlistView) this.findViewById(R.id.xlistview);
		mListView = xlistview.mListView;
		mListView.setDividerHeight(0);
		mListView.setPadding(0, 0, 0, 0);
		tip_top = findViewById(R.id.tip_top);
		group_name = (TextView) findViewById(R.id.group_name);
		discrip = (TextView) findViewById(R.id.discrip);
	}

	/***************
	 * 注册监听时间
	 ***************/
	public void setListener() {
		title_img.setOnClickListener(this);
		xlistview.setKKDScrollListener(this); // 注册下拉刷新和加载更多
	}

	public void whetherNetWork() {
		xlistview.startRefresh();

		setMyAdater();

		if (!AppUtil.isConnect(this)) {
			xlistview.stopRefresh();
			img_null.setImageResource(R.drawable.img_null_net);
			tv_null.setText("网络错误，请稍候下拉刷新重试");
			img_null.setVisibility(View.VISIBLE);
			tv_null.setVisibility(View.VISIBLE);
		}
	}

	/************
	 * 适配器
	 ************/
	public void setMyAdater() {
		adapter = new RegularAdapter(this, datalist);
		mListView.setAdapter(adapter);

		requestAllBid(); // 请求接口
	}

	/************
	 * 请求标的
	 ************/
	public void requestAllBid() {
		KkdHttpMagager.getInstance().tenderWelfare(new HttpCallBack() {

			@Override
			public void onSuccess(int statusCode, Object responseBody) {
				xlistview.setNoMore(true);
				datalist.clear();
				selectedGroup group0 = ((List<selectedGroup>) responseBody)
						.get(0);
				datalist.addAll(group0.getBidGrounp());
				group_name.setText(group0.getBidTitle());
				group_name.setBackgroundColor(Color.parseColor(group0
						.getGroupColor()));
				discrip.setText(group0.getBidDesc());
				tip_top.setVisibility(View.VISIBLE);
				mHandler.sendEmptyMessage(REFRESH);
			}

			@Override
			public void onFailure(int statusCode, String desc) {
				Message msg = Message.obtain();
				msg.what = REQUEST_WAS_ABORTED;
				msg.obj = desc;
				msg.arg1 = statusCode;
				mHandler.sendMessage(msg);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_img:
			finish();
			break;
		}

	}

	/****************
	 * 下拉刷新
	 ****************/
	@Override
	public void onRefresh() {
		adapter.mIsFirstAnimation = true;
		requestAllBid();
		REFRESHFLAG = true;
	}

	/***************
	 * 加载更多
	 ***************/
	@Override
	public void onLoadMore() {

	}

}